﻿using Acme.SmartWMS.BasicInfo;
using Acme.SmartWMS.BasicInfo.CommodityDetails;
using Acme.SmartWMS.Common;
using Acme.SmartWMS.DataBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.SmartWMS.Inbound.OtherInbounds
{
    /// <summary>
    /// 其他入库Service
    /// </summary>
    public class OtherInboundAppService : ApplicationService, IOtherInboundAppService
    {
        public readonly IRepository<OtherInbound, int> _repository;

        public OtherInboundAppService(IRepository<OtherInbound, int> repository)
        {
            _repository = repository;
           
        }

        public async Task<int> AddOtherInbound(OtherInboundDTO dto)
        {
            var entity = dto.MapTo<OtherInbound>();
            await _repository.InsertAsync(entity);
            return 1;
        }
        public async Task<int> UpdateOtherInbound(OtherInboundDTO dto)
        {
            var entity = dto.MapTo<OtherInbound>();
            await _repository.UpdateAsync(entity);
            return 1;
        }
        public async Task<int> DeleteOtherInbound(int id)
        {
            await _repository.DeleteAsync(x => x.Id == id);
            return 1;
        }

        public PagedResult GetOtherInbound(string? name, int page = 1, int limit = 2)
        {
            var predicate = PredicateBuilder.New<OtherInbound>(true);
            if (!string.IsNullOrEmpty(name))
            {
                predicate = predicate.And(m => m.Warehouse.Contains(name));
            }
            var dto = _repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
            result.PageSize = limit;
            result.CurrentPage = page;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        public async Task<OtherInbound> GetOtherInboundById(int id)
        {
            var model = await _repository.FirstOrDefaultAsync(x => x.Id == id);
            return model;
        }
    }
}
